package GUI;

import java.util.ArrayList;

import Control.Character;


public class CharacterCatchInfoPanel {

	public int x;
	public int y;
	public int width;
	public int height;
	public ScrollBar scrollBar;
	ArrayList<CharacterCatchInfoPanelData> cd;
	////////////////////////////////////////////////////////////////////////////////////////////////////

	public CharacterCatchInfoPanel(){
		x=Utils.globalProcessing.width-240;
		y=15;
		width = 230;
		height = Utils.globalProcessing.height/2 -30;
		cd = new ArrayList<CharacterCatchInfoPanelData>();
		scrollBar = new ScrollBar();
	}

	public void draw()
	{

		Utils.globalProcessing.fill(Utils.textColor);
		Utils.globalProcessing.textFont(Utils.plotFont, 15);
		Utils.globalProcessing.textAlign(Utils.globalProcessing.LEFT);
		Utils.globalProcessing.text("Selected Characters and Phrases", x , y + 10);
		// count lines needed to draw all the characters and their catch pharases
		int linesCount=0,lastCatchPhraseCount=0;
		for(int i=0;i<cd.size();i++){
			linesCount += (Utils.ccip.cd.get(i).pmButton.pluState ? 0 : 
				(Utils.ccip.cd.get(i).characterSelectedButtonPanel
						.c.getCatchphrases().size())+3) + 1;
			lastCatchPhraseCount = (Utils.ccip.cd.get(i).pmButton.pluState ? 0 : 
				(Utils.ccip.cd.get(i).characterSelectedButtonPanel
						.c.getCatchphrases().size())+3) + 1;

		}
		float j;
		if(((linesCount)*25+28)>height){
			Utils.ccip.scrollBar.width = 15;
			Utils.ccip.scrollBar.x = Utils.width-15;
			Utils.ccip.scrollBar.y = y+30;
			Utils.ccip.scrollBar.height = Utils.globalProcessing.height/2 -50;
			Utils.ccip.scrollBar.draw();  
			Utils.ccip.scrollBar.size = 1/(float)(Utils.ccip.cd.size());
			j = Main.map(Utils.ccip.scrollBar.val,0,1,0,Utils.ccip.cd.size());
		}
		else j=0;

		int k=0,tot=0,var;
		//draw character and catch phrases
		for(int i=(int)(j+0.5); i<Utils.ccip.cd.size();i++){
			if(((tot)*24+30)<height){
				PlusMinusButton pmb = Utils.ccip.cd.get(i).pmButton;
				pmb.x=x;
				pmb.y=y+30+(tot)*24 +6;
				pmb.draw();
				CharacterInfoPanelButton cb = Utils.ccip.cd.get(i).characterSelectedButtonPanel;
				cb.x=x+12;
				cb.y=y+30+(tot)*24;
				cb.width=100;
				cb.height=20;
				cb.draw();
				var=tot;
				//draw Icon
				Utils.ccip.cd.get(i).iconMiniButton.x=x+130+(i%2)*35;
				Utils.ccip.cd.get(i).iconMiniButton.y=y+15+(tot)*24 +6;
				Utils.ccip.cd.get(i).iconMiniButton.draw();
				tot++;

				if(!Utils.ccip.cd.get(i).pmButton.pluState){
					//draw first appearance line
					Utils.globalProcessing.textFont(Utils.plotFont, 13);
					try{
						if(((tot)*24+30)<height){
							Utils.globalProcessing.fill(255);
							Utils.globalProcessing.textFont(Utils.plotFont, 13);
							Utils.globalProcessing.text("First Appearance S"+Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.getFirstAppearence().getSeason().getNumber()+" E"+
									Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.getFirstAppearence().getNumEpisode(),x+20,y+45+(tot)*24);
							tot++;
						}
					}catch(Exception e ){System.out.println("no appearance for "+Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.getName());}
					//draw affine characters button
					if(((tot)*24+30)<height){

						Utils.ccip.cd.get(i).affineCharactersButton.x = x+15;
						Utils.ccip.cd.get(i).affineCharactersButton.y = y+30+(tot)*24;
						Utils.ccip.cd.get(i).affineCharactersButton.label = "Show Affine Characters";
						Utils.ccip.cd.get(i).affineCharactersButton.height =20;
						Utils.ccip.cd.get(i).affineCharactersButton.draw();
						tot++;
					}
					if(((tot)*24+30)<height){
						if(Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.getCommonWords().size()>0){
							//draw common words button
							Utils.ccip.cd.get(i).commonWordsButton.x = x+15;
							Utils.ccip.cd.get(i).commonWordsButton.y = y+30+(tot)*24;
							Utils.ccip.cd.get(i).commonWordsButton.label = "Show Common Words";
							Utils.ccip.cd.get(i).commonWordsButton.height = 20;
							Utils.ccip.cd.get(i).commonWordsButton.draw();
							tot++;
						}
					}
					if(Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.isMainCharacter()){

						if(((tot)*24+30)<height){
							Utils.globalProcessing.text("Catch Phrases:",x+20, y+45+(tot)*24);
							tot++;
						}
						//draw catch phrases
						for(k=0;k<Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.size() && ((tot)*24+30)<height;k++){
							CatchPhraseInfoPanelButton cpb = Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(k);
							cpb.x=x+25;
							cpb.y=y+30+(tot)*24;
							cpb.width=100;
							cpb.height=20;
							cpb.draw();
							tot++;
						}
					}
				}

				//draw remove character button
				cb.closeButton.x=Utils.width-35;
				cb.closeButton.y=y+30+(var)*24 +6;
				cb.closeButton.draw();
			}
		}

	}

	public void removeCharacterSelected(Character c) {
		Utils.characterSelected.remove(c);
		//remove the character and his catch phrases from those to be displayed
		int i;
		for(i =0;i<Utils.ccip.cd.size();i++){
			if(Utils.ccip.cd.get(i).characterSelectedButtonPanel.label.equals(c.getShortName())){
				Utils.ccip.cd.remove(i);
				break;
			}

		}

		for(i=Utils.catchPhrasesSelected.size()-1;i>=0;i--){
			for(int j=0;j<c.getCatchphrases().size();j++){
				if(Utils.catchPhrasesSelected.get(i).equals(c.getCatchphrases().get(j))){
					Utils.catchPhrasesSelected.remove(i);
					i=Utils.catchPhrasesSelected.size()-1;
					break;
				}
			}
		}
		Utils.graphArea.createLineGraphPoints();

	}
	public void addCharacterSelected(Character c){
		if(!Utils.characterSelected.contains(c)){
			Utils.characterSelected.add(c);
			CharacterCatchInfoPanelData characterData = new CharacterCatchInfoPanelData(c);
			characterData.characterSelectedButtonPanel.selected=true;

			for(int i=0;i<c.getCatchphrases().size();i++){
				CatchPhraseInfoPanelButton cpb = new CatchPhraseInfoPanelButton();
				cpb.setLabel(c.getCatchphrases().get(i).getPhrase());
				cpb.catchPhrase = c.getCatchphrases().get(i);
				cpb.selected=false;
				characterData.catchPhraseSelectedButtonPanel.add(cpb);
			}
			Utils.ccip.cd.add(characterData);
			Utils.graphArea.createLineGraphPoints();
		}


	}

	public void removeOtherCatchPhrases(
			CatchPhraseInfoPanelButton catchPhraseInfoPanelButton) {
		for(int i=0;i<Utils.ccip.cd.size();i++){
			if(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.contains(catchPhraseInfoPanelButton)){
				for(int j=0;j<Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.size();j++){
					if(!catchPhraseInfoPanelButton.catchPhrase.equals(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).catchPhrase)){
						Utils.catchPhrasesSelected.remove(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).catchPhrase);
						Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).selected=false;
					}
				}
			}
		}

	}

	public void removeOtherCatchPhrases(
			CatchPhraseButton catchPhraseButton) {
		for(int i=0;i<Utils.ccip.cd.size();i++){
			if(Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.equals(catchPhraseButton.c.getCharacter()))
				for(int j=0;j<Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.size();j++){
					if(!catchPhraseButton.c.equals(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).catchPhrase)){
						Utils.catchPhrasesSelected.remove(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).catchPhrase);
						Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).selected=false;
					}
				}

		}

	}

	public String getSelectedCatchPhrase(Character character) {
		for(int i=0;i<Utils.ccip.cd.size();i++){
			if(Utils.ccip.cd.get(i).characterSelectedButtonPanel.c.equals(character)){
				for(int j=0;j<Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.size();j++){
					if(Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).selected)
						return Utils.ccip.cd.get(i).catchPhraseSelectedButtonPanel.get(j).label;
				}
			}
		}

		return new String("Not Found");
	}



}
